package ru.aslanov.schedule.client.util;

/**
 * Created by IntelliJ IDEA.
 * Created: Feb 8, 2010 7:13:28 PM
 *
 * @author Sergey Aslanov
 */
public class JavaScriptMethodHelper {
    private static int requestCounter = 0;

    public static String registerCallbackFunction(JavaScriptMethodCallback callback) {
        String callbackName = "callback" + (requestCounter++);
        return registerCallbackFunction(callbackName, callback);
    }

    public static String registerCallbackFunction(String callbackName, JavaScriptMethodCallback callback) {
        createCallbackFunction(callback, callbackName);
        return callbackName;
    }

    private native static void createCallbackFunction(JavaScriptMethodCallback obj, String callbackName)/*-{
	tmpcallback = function( j ){
		obj.@ru.aslanov.schedule.client.util.JavaScriptMethodCallback::execute(Lcom/google/gwt/core/client/JavaScriptObject;)( j );
	};
	$wnd[callbackName]=tmpcallback;
}-*/;

}

